package com.geotmt.billingcenter.modules.cost.controller;

import com.geotmt.billingcenter.common.exception.MyException;
import com.geotmt.billingcenter.common.result.ResponseResult;
import com.geotmt.billingcenter.common.vo.cost.CostHisVo;
import com.geotmt.billingcenter.common.vo.cost.CostVo;
import com.geotmt.billingcenter.common.vo.params.PageOrgFilter;
import com.geotmt.billingcenter.common.vo.params.ParamsCost;
import com.geotmt.billingcenter.common.vo.params.ParamsForDel;
import com.geotmt.billingcenter.modules.cost.service.CostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 成本Controller
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/20 15:25
 * @Version     v1
 **/
@Api(value = "成本配置接口",tags = "成本配置接口")
@RestController
@RequestMapping(value = "/bc/cost")
public class CostController extends PageOrgFilter {

    private Logger logger = LoggerFactory.getLogger(CostController.class);

    @Resource
    private CostService costService;

    /**
     * 添加 规则配置
     * @return
     */
    @ApiOperation(value = "ADD CURR 配置（批量）")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseResult addCost(@RequestBody List<CostVo> costVoList,HttpServletRequest request){
        logger.info("添加规则配置,costVoList:{}",costVoList);
        try{
            if(null != costVoList && !costVoList.isEmpty()){
                for(CostVo costVo : costVoList){
                    if(null == costVo.getDsId() || null == costVo.getOutInterfaceId() || null == costVo.getLabel() || null == costVo.getFeeFlag()){
                        return ResponseResult.build(HttpServletResponse.SC_NOT_ACCEPTABLE,"数据源，外部接口,接口类别，计费模式 不能为空",null,false);
                    }
                    costVo.setCreateUserId(getUserId(request));
                    costVo.setCreateTime(new Date());
                }
            }
            costService.addList(costVoList);
        }catch (MyException e){
            logger.error("添加最新规则失败,Exception:",e);
            return ResponseResult.build(e.getCode(),e.getMsg(),null,false);
        }catch (Exception e) {
            logger.error("添加最新规则失败,Exception:",e);
            return ResponseResult.error("添加失败");
        }
        return ResponseResult.ok("添加成功");
    }

    /**
     * 批量添加修改规则配置
     * @return
     */
    @ApiOperation(value = "ADD/UPDATE  规则配置（批量）")
    @RequestMapping(value = "/addUpdateCostList", method = RequestMethod.POST)
    public ResponseResult addUpdateCost(@RequestBody List<CostVo> costVoList, HttpServletRequest request){
        logger.info("成本批量添加修改规则配置,costVoList:{}",costVoList);
        try{
            costService.addUpdateList(costVoList, request);
        }catch (MyException e){
            logger.error("成本批量添加修改规则配置失败");
            return ResponseResult.build(e.getCode(),e.getMsg(),null,false);
        }catch (Exception e) {
            logger.error("成本批量添加修改规则配置失败",e);
            return ResponseResult.error("成本批量添加修改规则配置失败");
        }
        return ResponseResult.ok("成本批量添加修改规则配置成功");
    }

    /**
     * 分析List规则配置
     * @return
     */
    @ApiOperation(value = "成本分析List规则配置")
    @RequestMapping(value = "/analyzeCostList", method = RequestMethod.POST)
    public ResponseResult analyzeCostList(@RequestBody List<CostVo> costVoList, HttpServletRequest request){
        logger.info("成本分析List规则配置,costVoList:{}",costVoList);
        Map<String, Object> resultMap;
        try{
            resultMap = costService.analyzeList(costVoList);
        } catch (Exception e) {
            logger.error("成本分析List规则配置失败",e);
            return ResponseResult.error("成本分析List规则配置失败");
        }
        return ResponseResult.build(HttpServletResponse.SC_OK,"成本分析List规则配置成功",resultMap,true);
    }

    /**
     * 修改规则配置
     * @param costVo
     * @return
     */
    @ApiOperation(value = "UPDATE CURR 配置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateCost(@RequestBody CostVo costVo,HttpServletRequest request){
        logger.info("修改规则配置,costVo:{}",costVo);
        try{
            if(null == costVo.getId()){
                return ResponseResult.build(HttpServletResponse.SC_NOT_ACCEPTABLE,"id不能为空",null,false);
            }
            costVo.setUpdateUserId(getUserId(request));
            costVo.setUpdateTime(new Date());
            costService.update(costVo);
        }catch (MyException e){
            logger.error("修改最新规则失败,Exception:",e);
            return ResponseResult.build(e.getCode(),e.getMsg(),null,false);
        }catch (Exception e) {
            logger.error("修改最新规则失败,Exception:",e);
            return ResponseResult.error("修改失败");
        }
        return ResponseResult.ok("修改成功");
    }

    /**
     * 删除
     * @param obj
     * @return
     */
    @ApiOperation(value = "DELETE CURR 配置")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteCost(@RequestBody ParamsForDel obj,HttpServletRequest request){
        logger.info("删除规则配置,obj:{}",obj);
        try{
            if(null == obj.getId()){
                return ResponseResult.build(HttpServletResponse.SC_NOT_ACCEPTABLE,"id不能为空",null,false);
            }
            obj.setUpdateUserId(getUserId(request));
            obj.setUpdateTime(new Date());
            costService.delete(obj);
        }catch (MyException e){
            logger.error("删除最新规则失败 ");
            return ResponseResult.build(e.getCode(),e.getMsg(),null,false);
        }catch (Exception e) {
            logger.error("删除最新规则失败,Exception:",e);
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 最新规则List
     * @param obj
     * @return
     */
    @ApiOperation(value = "CURR List")
    @RequestMapping(value = "/listCurr", method = RequestMethod.POST)
    public ResponseResult listCurr(@RequestBody ParamsCost obj){
        logger.info("最新规则 list,ParamsConsume:{}",obj);
        filterPage(obj,"curr.id desc");
        PageInfo<CostVo> pageInfo;
        try{
            pageInfo = new PageInfo<>(costService.list(obj));
        }catch (Exception e) {
            logger.error("最新规则 list 查询失败,Exception:",e);
            return ResponseResult.error("查询失败");
        }
        return ResponseResult.build(HttpServletResponse.SC_OK,"查询成功",pageInfo,true);
    }

    /**
     * 添加或修改的回显List
     * @param obj
     * @return
     */
    @ApiOperation(value = "ListAll add/update 回显接口")
    @RequestMapping(value = "/listAllCost", method = RequestMethod.POST)
    public ResponseResult listAllCost(@RequestBody ParamsCost obj){
        logger.info("成本添加或修改的回显list,ParamsCost:{}",obj);
        List<CostVo> consumeVos;
        try{
            consumeVos = costService.listAll(obj);
        }catch (Exception e) {
            logger.error("成本添加或修改的回显 list 查询失败",e);
            return ResponseResult.error("成本添加或修改的回显list查询失败");
        }
        return ResponseResult.build(HttpServletResponse.SC_OK,"成本添加或修改的回显list查询成功",consumeVos,true);
    }

    /**
     * 历史规则List
     * @param obj
     * @return
     */
    @ApiOperation(value = "HIS List")
    @RequestMapping(value = "/listHis", method = RequestMethod.POST)
    public ResponseResult listHis(@RequestBody ParamsCost obj){
        logger.info("历史规则 list,ParamsConsume:{}",obj);
        filterPage(obj,"currHis.effect_start_time desc");
        PageInfo<CostHisVo> pageInfo;
        try{
            pageInfo = new PageInfo<>(costService.listHis(obj));
        }catch (Exception e) {
            logger.error("历史规则 list 查询失败,Exception:",e);
            return ResponseResult.error("查询失败");
        }
        return ResponseResult.build(HttpServletResponse.SC_OK,"查询成功",pageInfo,true);
    }

}
